﻿#ifndef CHATSERVER_H
#define CHATSERVER_H

#include <QTcpServer>
#include <QTcpSocket>

class ChatServerPrivate;

class ChatServer : public QTcpServer {
  Q_OBJECT
public:
  ChatServer(QObject *parent = nullptr);
  ~ChatServer();

public:
  void incomingConnection(qintptr handle);

public:
  void setTcpServer(QString ip, int port);

  bool startListen();
  bool stopListen();

  void sendToClient(int index, QByteArray &msg);
  void sendToAllClient(QByteArray &msg);

  void receiveFromClient(const QByteArray &rawMsg);

signals:
  void sendMsg(const QByteArray &msg);

  void closeClient();

  void receiveMsg(const QString &ip, const QString &port,
                  const QByteArray &rawMsg);

private:
  void disconnectToClient();

private:
  QScopedPointer<ChatServerPrivate> serverPrivate;
};

#endif // CHATSERVER_H
